package{
	import com.asnike.navi.StepNavigation;
	import com.asnike.net.ASLoader;
	import com.asnike.selector.BasicSelector;
	import com.asnike.selector.FinalStep;
	import com.asnike.selector.GallerySelector;
	import com.asnike.selector.ItemSelector;
	import com.asnike.selector.MapSelector;
	import com.asnike.selector.SaveSelector;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.Font;
	
	[SWF(width="795", height="860")]
	public class Main extends Sprite{
		
		include 'embed/font.as';
		
		private var _data:XML;
		private var _navigation:StepNavigation;
		private var _step0:GallerySelector;
		private var _step1:GallerySelector;
		private var _step2:GallerySelector;
		private var _step3:MapSelector;
		private var _step4:ItemSelector;
		private var _step5:SaveSelector;
		private var _stepFinal:FinalStep;
		
		public function Main(){
			initFont();
			loadXMLData();
			loadResource();
		}
		private function initFont():void{
			Font.registerFont( Font0 );
			Font.registerFont( Font1 );
		}
		private function loadXMLData():void{
			ASLoader.loadXML( 'remote/data.xml', function( $result:Array ):void{
				_data = $result[0];
				
				initStep012();
			} );
		}
		private function initStep012():void{
			_step0 = new GallerySelector;
			_step0.setData( _data.step[0] );
			_step0.setImageClickCallback( step0ImageClick );
			_step0.x = 17;
			_step0.y = 87;
			addChild( _step0 );
			
			_step1 = new GallerySelector;
			_step1.setData( _data.step[1] );
			_step1.setImageClickCallback( step1ImageClick );
			_step1.x = 17;
			_step1.y = 87;
			_step1.hide();
			addChild( _step1 );
			
			_step2 = new GallerySelector;
			_step2.setData( _data.step[2] );
			_step2.setImageClickCallback( step2ImageClick );
			_step2.x = 17;
			_step2.y = 87;
			_step2.hide();
			addChild( _step2 );
		}
		private function step0ImageClick( $index:int ):void{
			_step0.select( $index );
			_step0.hide();
			_step1.show();
			_navigation.selectButton( 1 );
			_navigation.setSelection( 0, $index );
			trace( $index );
		}
		private function step1ImageClick( $index:int ):void{
			_step1.select( $index );
			_step1.hide();
			_step2.show();
			_navigation.selectButton( 2 );
			_navigation.setSelection( 1, $index );
		}
		private function step2ImageClick( $index:int ):void{
			_step2.select( $index );
			_step2.hide();
			_step3.show();
			_navigation.selectButton( 3 );
			_navigation.setSelection( 2, $index );
		}
		
		private function loadResource():void{
			var loader:Loader;
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function( $e:Event ):void{
				$e.target.removeEventListener( $e.type, arguments.callee );
				initStepNavigation();
				initStep345();
				initFinalStep();
				initUpperImages();
			} );
			loader.load( new URLRequest( 'remote/resource.swf' ), new LoaderContext( false, ApplicationDomain.currentDomain ) );
		}
		private function initStepNavigation():void{
			_navigation = new StepNavigation;
			_navigation.setBtnClickCallback( naviButtonClick );
			addChild( _navigation );
		}
		private function naviButtonClick( $index:int ):void{
			switch( $index ){
			case 0:
				_step0.show();
				_step1.hide();
				_step2.hide();
				_step3.hide();
				_step4.hide();
				_step5.hide();
				_stepFinal.hide();
				break;
			case 1:
				_step1.show();
				_step0.hide();
				_step2.hide();
				_step3.hide();
				_step4.hide();
				_step5.hide();
				_stepFinal.hide();
				break;
			case 2:
				_step2.show();
				_step0.hide();
				_step1.hide();
				_step3.hide();
				_step4.hide();
				_step5.hide();
				_stepFinal.hide();
				break;
			case 3:
				_step0.hide();
				_step1.hide();
				_step2.hide();
				_step3.show();
				_step4.hide();
				_step5.hide();
				_stepFinal.hide();
				break;
			case 4:
				_step0.hide();
				_step1.hide();
				_step2.hide();
				_step3.hide();
				_step4.show();
				_step5.hide();
				_stepFinal.hide();
				break;
			case 5:
				_step0.hide();
				_step1.hide();
				_step2.hide();
				_step3.hide();
				_step4.hide();
				_step5.show();
				_stepFinal.hide();
				break;
			}
			_navigation.selectButton( $index );
		}
		
		private function initStep345():void{
			_step3 = new MapSelector( _data.step[3].places[0] );
			_step3.y = 87;
			_step3.hide();
			_step3.setClickBtnNextCallback( clickBtnNext );
			addChild( _step3 );
			
			_step4 = new ItemSelector;
			_step4.setData( _data.step[4] );
			_step4.y = 87;
			_step4.hide();
			_step4.setClickBtnNextCallback( clickBtnNext );
			addChild( _step4 );
			
			_step5 = new SaveSelector;
			_step5.y = 87;
			_step5.hide();
			_step5.setCompleteCallback( completeAllStep );
			addChild( _step5 );
		}
		private function clickBtnNext( $step:int ):void{
			naviButtonClick( $step + 1 );
		}
		private function completeAllStep():void{
			_stepFinal.show();
			_stepFinal.setStep0Result( _step0.getResult() );
			_stepFinal.setStep1Result( _step1.getResult() );
			_stepFinal.setStep2Result( _step2.getResult() );
			_stepFinal.setStep3Result( _step3.getResult() );
			_stepFinal.setStep4Result( _step4.getResult() );
		}
		private function initFinalStep():void{
			_stepFinal = new FinalStep;
			_stepFinal.hide();
			addChild( _stepFinal );
		}
		private function initUpperImages():void{
			_step0.setUpperImage( 'step1_title' );
			_step1.setUpperImage( 'step2_title' );
			_step2.setUpperImage( 'step3_title' );
			_step3.setUpperImage( 'step4_title' );
			_step4.setUpperImage( 'step5_title' );
		}
	}
}